home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-06-24 | 3.2 KB | 115 lines | [TEXT/Help] |
- {••• A simple Constraint solver: Uses a "Backtrack" algorithm •••}
- { To use it: simply open one of the example files in this folder
- and call the function (c) w/o any argument, answer questions }
-
- {The main func: an environment, var list, domain list, constraint list}
-
- (define (try env lv ld lc)
- (cond (null? lv)'()
- (let [(ne (maj env (0 lv) (0(0 ld))))]
- (cond (tlc (0 lc) ne)
- (begin (printsol ne lv lc)
- (try ne (-1 lv) (-1 ld) (-1 lc))))
- (cond (cons? (-1(0 ld)))
- (try env lv (cons (-1 (0 ld)) (-1 ld)) lc)))))
-
- ;---- copy and upadte an env.
- (define (maj env var val)
- (binding=! var (bcopy env) val))
-
- ;---- check is all constraints are satisfied in the env
- (define (tlc lc env)
- (cond (null? lc) †
- (eval (0 lc) env) (tlc (-1 lc) env)))
-
- ;---- prints the solution, if any
- (define (printsol env lv lc)
- (cond (null? (-1 lv)) (begin (prinio "Solution:" stder)
- (prinio env stder)
- (prinio "
- " stder)
- (flushio stder))))
-
- ;---- I/F user
-
- (define (c)
- (let [(lv (begin (prin "Variables list: ") (flushio stdo)(read)))
- (lc (begin (prin "Constraints list:")(flushio stdo)(read)))
- (ld (begin (prin "Domains list: ") (flushio stdo)(read)))]
- (try (apply makeenv lv) lv ld (créelvc lv (process lc lv) '()))))
-
- ;---- seek o in s (deep search)
- (define (findall o s)
- (cond (eq? o s) †
- (not (cons? s)) ƒ
- (findall o (0 s)) †
- (findall o (-1 s))))
-
- ;---- extract variables constrained by the constraints
- ;---- returns a list of conses (lv | cont)
- (define (process lc lv)
- (cond (null? lc) ()
- (cons (cons (extract (0 lc) lv '()) (0 lc))
- (process (-1 lc) lv))))
-
- (define (extract c lv bag)
- (cond (null? lv) bag
- (findall (0 lv) c) (extract c (-1 lv) (cons (0 lv) bag))
- (extract c (-1 lv) bag)))
-
- ;---- is in
- (define (isinq el l)
- (cond (null? l) ƒ
- (eq? el (0 l)) †
- (isinq el (-1 l))))
-
-
- ;---- is included
- (define (isincluded e1 e2)
- (cond (null? e1) †
- (isinq (0 e1) e2) (isincluded (-1 e1) e2)))
-
- ;---- built the list var constraints
- (define (créelvc lv lc b0)
- (cond (null? lv) '()
- (let [(x (trclv (0 lv) lc (cons (0 lv) b0) '() '()))]
- (cons (-1 x)
- (créelvc (-1 lv) (0 x) (cons (0 lv) b0))))))
-
- (define (trclv v nlc e b1 b2)
- (cond (null? nlc) (cons b2 b1)
- (isincluded (0(0 nlc)) e) (trclv v (-1 nlc) e (cons (-1(0 nlc)) b1) b2)
- (trclv v (-1 nlc) e b1 (cons (0 nlc) b2))))
-
- ;---- application of a binary op. to each couple in lv
- (define (genbin sym lv)
- (cond (null? lv) ()
- (append (mapcar1 (lambda(x) (list sym (0 lv) x)) (-1 lv))
- (genbin (-1 lv)))))
-
- (define (mapcar1 f l)
- (cond (null? l)()
- (cons (f (0 l))
- (mapcar1 f (-1 l)))))
-
- ;---- Propositionnal Logic
-
- (define (ou a b)
- (cond (=? a 0) b 1))
-
- (define (non a)
- (cond (=? a 0) 1 0))
-
- (define (et a b)
- (cond (=? a 0) 0 b))
-
- (define (implique a b)
- (cond (=? a 0) 1 b))
-
- (define (vrai? a)
- (=? a 1))
-
- (define (faux? a)
- (=? a 0))
-
-